﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Configuration;
using APMS.WebSite.Common;
using MySql.Data.MySqlClient;

namespace APMS.WebSite.Models.DataStore
{
    /// <summary>
    /// DataStoreFactory is the factory for creating polyphomic DataStore 
    /// </summary>
    /// <version>$Revision$ $Date$</version>
    /// <history>
    /// <record>08/27/2008. raQuiam - Created this file</record>
    /// </history>
    public static class DataStoreFactory
    {
        #region Public Methods
        /// <summary>
        /// Create the instance of DataStore from currently used DataProvider
        /// </summary>
        /// <returns></returns>
        public static IDataStore GetDataStore()
        {
            switch (ApplicationConfig.DataProvider)
            {
                case Constants.AppSettings.DataProvider.MYSQL:
                    return GetMySQLDataStore();
                case Constants.AppSettings.DataProvider.ORACLE:
                    break;
                default:
                    // TODO: Throw ApmsException - no DataProvider specified
                    throw new ApplicationException("ApmsException - no DataProvider specified");
            }

            return null;
        }
        #endregion

        #region Helper Methods
        /// <summary>
        /// Create an instance of MySQLDataStore
        /// </summary>
        /// <returns></returns>
        public static MySQLDataStore GetMySQLDataStore()
        {
            string connectionString = ((ConnectionStringSettings)WebConfigurationManager.ConnectionStrings[Constants.ConnectionStrings.MYSQL]).ConnectionString;

            // TODO: Throw ApmsException - empty connectionString
            if (String.IsNullOrEmpty(connectionString))
                throw new ApplicationException("ApmsException - empty connectionString");

            MySqlConnection connection = new MySqlConnection(connectionString);

            return new MySQLDataStore(connection);
        }

        // TODO: CreateOracleDataStore()
        #endregion

    }
}
